local jd = Package:new("jd_test")

jd.extensionName = "Super_Skeld"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["jd"] = "Skeld",
  ["sk"] = "Skeld",
  ["sk_n"] = "Re:Skeld",
  ["sk_d"] = "Skeld异画",
  ["jd_test"] = "Skeld 测试服",
  ["Super_Skeld_test"] = "Skeld 测试服",
}






local sk_tlp = General:new(jd, "sk_tlp", "skeld", 4, 4,General.Male)

local getSkills = function (p)
  local skills = {}
  for _, s in ipairs(p.player_skills) do
    if s:isPlayerSkill(p) and s.visible then
      table.insertIfNeed(skills, s.name)
    end
  end
  return skills
end




local tlp_jieyin = fk.CreateTriggerSkill{
  name = "tlp_jieyin",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing,fk.EventPhaseChanging},
  can_trigger = function(self, event, target, player, data)
    if data.to == Player.Start then
      return target == player and player:hasSkill(self) and #player:getTableMark(self.name) >= 9
    end
    if event == fk.CardUsing then
      return target == player and player:hasSkill(self)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event== fk.CardUsing then
      local hand = player:getCardIds(Player.Hand)
      local cz = {}
      for _, id in ipairs(hand) do
        if data.card.trueName == Fk:getCardById(id).trueName then
          table.insertIfNeed(cz,id)
        end
      end
      local jy_draw_by_recast
      if #cz > 0 then
        jy_draw_by_recast = room:recastCard(cz, target, self.name)
        room:addPlayerMark(player,"jiejin_1-turn",#jy_draw_by_recast)
      else
        player:drawCards(1,self.name)
        room:addPlayerMark(player,"jiejin_1-turn",1)
      end
      if not table.contains(player:getTableMark(self.name), data.card.trueName) then
        player.room:addTableMark(player, self.name, data.card.trueName)
      end
      local jiejin_ex = player:getTableMark(self.name)
      local jiejin_1 = player:getMark("jiejin_1-turn")
      local jiejin_2 = #getSkills(player)
      room:setPlayerMark(player,"jiejin_2-turn",jiejin_2)
      if jiejin_1 >= jiejin_2 then
        jiejin_1 = jiejin_2
        room:handleAddLoseSkills(player,"tlp_jieyin_y")
        if player:getMark("jiejin_anim-turn") ~= 1 then
          room:setPlayerMark(player,"jiejin_anim-turn",1)
          room:notifySkillInvoked(player, self.name, "switch")
        end
      end
      local show = jiejin_1.."/"..jiejin_2.."["..#jiejin_ex.."]"
      room:setPlayerMark(player,"@jiejin_show-turn",show)
    else
      room:notifySkillInvoked(player, self.name, "big")
      player:drawCards(2,self.name)
      room:setPlayerMark(player,"@jiejin_show-turn",0)
      room:setPlayerMark(player,"jiejin_2-turn",0)
      room:setPlayerMark(player,"jiejin_1-turn",0)
      room:setPlayerMark(player,"tlp_jieyin",0)
      room:setPlayerMark(player,"@jiejin_show-turn",0)
      room:handleAddLoseSkills(player,"tlp_jieyin_active|-tlp_jieyin")
      room:setPlayerMark(player,"jiejin_xiugai",1)
    end
  end,
}

local tlp_jieyin_y = fk.CreateTriggerSkill{
  name = "#tlp_jieyin_y",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing},
  priority = 0.1,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return target == player and player:hasSkill(self)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not table.contains(player:getTableMark("tlp_jieyin"), data.card.trueName) then
      player.room:addTableMark(player, "tlp_jieyin", data.card.trueName)
    end
    local jiejin_ex = player:getTableMark("tlp_jieyin")
    local jiejin_1 = player:getMark("jiejin_1-turn")
    local jiejin_2 = #getSkills(player)
    room:setPlayerMark(player,"jiejin_2-turn",jiejin_2)
    if jiejin_1 > jiejin_2 then
      jiejin_1 = jiejin_2
      if player:getMark("jiejin_anim-turn") ~= 1 then
        room:setPlayerMark(player,"jiejin_anim-turn",1)
        room:notifySkillInvoked(player, self.name, "switch")
      end
    end
    local show = jiejin_1.."/"..jiejin_2.."["..#jiejin_ex.."]"
    room:setPlayerMark(player,"@jiejin_show-turn",show)
  end,
}



local tlp_jieyin_Invalidity = fk.CreateInvaliditySkill {
  name = "#tlp_jieyin_Invalidity",
  invalidity_func = function(self, from, skill)
    local jiejin_2 = #getSkills(from)
    if from.general == "sk_tlp" and from:getMark("jiejin_xiugai") ~= 1 then
      if from:getMark("jiejin_1-turn") < jiejin_2 then
        return not (not skill:isPlayerSkill(from) or skill.trueName == "tlp_jieyin")
      else
        return skill.trueName == "tlp_jieyin"
      end
    else
      return nil
    end
  end,
}
















local jieyin_viewas = fk.CreateViewAsSkill{
  name = "jieyin_viewas",
  interaction = function()
    local all_names = U.getAllCardNames("t")
    local names = U.getViewAsCardNames(Self, "tlp_jieyin_xg", all_names, {}, Self:getTableMark("jyviewas-turn"))
    if #names == 0 then return false end
    return UI.ComboBox { choices = names, all_choices = all_names }
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = "tlp_jieyin_xg"
    return card
  end,
}

Fk:addSkill(jieyin_viewas)

local tlp_jieyin_xg = fk.CreateTriggerSkill{
  name = "#tlp_jieyin_xg",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing},
  priority = 0.1,
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return target == player and player:hasSkill(self) and
      not data.card:isVirtual()
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room

    if not table.contains(player:getTableMark("jieyin_xg_table-round"), data.card.trueName) then
      room:addTableMark(player, "jieyin_xg_table-round", data.card.trueName)
    end
    local jiejin_ex = player:getTableMark("jieyin_xg_table-round")
    local show = #jiejin_ex
    room:setPlayerMark(player,"@jiejin_show-turn",show)

    local h1 = #player:getLastAlive(true,1):getCardIds(Player.Hand)
    local h2 = #player:getCardIds(Player.Hand)
    local h3 = #player:getNextAlive(true,1):getCardIds(Player.Hand)
    local jiejin_2 = #getSkills(player)

    if show > jiejin_2 then
      if player:getMark("jiejin_anim-turn") ~= 1 then
        room:setPlayerMark(player,"jiejin_anim-turn",1)
        room:notifySkillInvoked(player, self.name, "switch")
      end
    end


    if h1 < h2 and #player:getMark("jieyin_xg_table-round") > jiejin_2 then
      local toAsk = table.filter(room.alive_players, function(p) return table.find(p:getCardIds("he"), function (id)
        return not p:prohibitDiscard(id) and (p.id == player:getLastAlive(true,1).id or p.id == player.id)
      end) end)
      local cardsMap = U.askForJointCard(toAsk, 1, 1, true, self.name, false, ".|.|.|hand,equip", "#AskForDiscard:::1:1", nil, true)
      local moveInfos = {}
      local chosen = {}
      for _, p in ipairs(toAsk) do
        local throw = cardsMap[p.id][1]
        if throw then
          table.insert(chosen, throw)
          table.insert(moveInfos, {
            ids = {throw},
            from = p.id,
            toArea = Card.DiscardPile,
            moveReason = fk.ReasonDiscard,
            proposer = p.id,
            skillName = self.name,
          })
        end
      end
      room:moveCards(table.unpack(moveInfos))
    end
    if h2 < h3 and #player:getMark("jieyin_xg_table-round") > jiejin_2 then
      local success, dat = player.room:askForUseActiveSkill(player, "jieyin_viewas", "#jieyin-invoke", true)
      if success then
        local room = player.room
        local card_name = dat.interaction
        local card = Fk:cloneCard(card_name)
        card.skillName = self.name
        room:addTableMark(player, "jyviewas-turn", card_name)
        room:useCard{
          from = player.id,
          tos = table.map(dat.targets, function(id) return {id} end),
          card = card,
        }
      end
    end
    return true
  end,
}



local tlp_jieyin_xg_Invalidity = fk.CreateInvaliditySkill {
  name = "#tlp_jieyin_Invalidity",
  invalidity_func = function(self, from, skill)
    local jiejin_2 = #getSkills(from)
    if from.general == "sk_tlp" and from:getMark("jieyin_xg_table-round") ~= 0 then
      if #from:getMark("jieyin_xg_table-round") >= jiejin_2 then
        return not (not skill:isPlayerSkill(from) or skill.trueName == "tlp_jieyin_active" or skill.trueName == "#tlp_jieyin_xg")
      end
    else
      return nil
    end
  end
}

local tlp_jieyin_active = fk.CreateActiveSkill{
  name = "tlp_jieyin_active",
  anim_type = "switch",
  prompt = "#jieyin_active-invoke",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 1 and not player:isKongcheng() and player:getHandcardNum() > 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = player.room:askForCard(player, 1, 1, true, self.name, false, "", "#jieyin_active-throw")
    local room = player.room
    room:throwCard({cards}, self.name, player, player)
    if player:isAlive() then
      local card = room:getCardsFromPileByRule(Fk:getCardById(cards[1]).trueName,drawPile)
      room:moveCards({
        ids = card,
        from = player.id,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = self.name,
      })
    local to = room:askForChoosePlayers(player,table.map(room:getOtherPlayers(player), Util.IdMapper),1,1,"#jiejin_chooseplayer",self.name,true)
      if #to ~= 0 then
        to = room:getPlayerById(to[1])
        local a,b
        if to.maxHp > #to.player_cards[Player.Hand] then
            a = to.maxHp - #to.player_cards[Player.Hand]
            if a ~= 0 then
            to:drawCards(a, self.name)
            end
        else
            a = -to.maxHp + #to.player_cards[Player.Hand]
            if a ~= 0 then
            to.room:askForDiscard(to, a, a, false, self.name, false, ".", "#jiejin_-discard:::"..a)
            end
        end

        if player.hp > #player.player_cards[Player.Hand] then
            b = player.hp - #player.player_cards[Player.Hand]
            if b ~= 0 then
            player:drawCards(b, self.name)
            end
        else
            b = -player.hp + #player.player_cards[Player.Hand]
            if b ~= 0 then
            player.room:askForDiscard(player, b, b, false, self.name, false, ".", "#jiejin_-discard:::"..b)
            end
        end
        if a == nil then
            a = 0
        end
        if b == nil then
            b = 0
        end
        if (a + b) >= player.maxHp then
            room:loseHp(player,1,self.name)
        end
      end
    end
  end,
}




local kaihe = fk.CreateActiveSkill{
  name = "kaihe",
  anim_type = "big",
  prompt = "#kaihe-invoke",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) < 1
  end,
  card_filter = Util.FalseFunc,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    return
      #selected == 0 and
      to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    U.viewCards(player, target.player_cards[Player.Hand], self.name, "#ViewCardsFrom:"..target.id)
    local skillsMap = getSkills(target)
    local skill = room:askForChoice(player, skillsMap, self.name, "#kaihe-choice", true)
    room:handleAddLoseSkills(player,skill)
  end,
}


tlp_jieyin:addRelatedSkill(tlp_jieyin_Invalidity)
tlp_jieyin:addRelatedSkill(tlp_jieyin_y)

tlp_jieyin_active:addRelatedSkill(tlp_jieyin_xg_Invalidity)
tlp_jieyin_active:addRelatedSkill(tlp_jieyin_xg)

sk_tlp:addSkill(tlp_jieyin)
sk_tlp:addSkill(kaihe)
sk_tlp:addRelatedSkill(tlp_jieyin_active)

Fk:loadTranslationTable{
  ["sk_tlp"] = "塔罗牌 废案",
  ["#sk_tlp"] = "御驾亲征",
  ["designer:sk_tlp"] = "塔罗牌",
  ["cv:sk_tlp"] = "塔罗牌",
  ["illustrator:sk_tlp"] = "塔罗牌",


  ["tlp_jieyin"] = "解印",
  ["@jiejin_show-turn"] = "解印",
  [":tlp_jieyin"] = " 使命技，当你使用牌时，若你的手牌中：有同名牌：你重铸你的所有同名牌；没有同名牌：你摸一张牌。当你每回合以此法摸了至少X张牌：前/时：你的其他技能失效/此技能本回合失效。（X为你武将牌上的技能数）<br>"..
  "成功：准备阶段，若你已经累计使用9张不同牌名的牌，你修改<a href='herf_tlp_jieyin_xg'>解印</a>并摸两张牌",

  ["herf_tlp_jieyin_xg"] = "①锁定技，当你使用牌时，若此时你的手牌数大于/小于你的上家/下家，且此牌有实体：你与其各弃置一张牌/你视为使用一张本回合未以此法使用过的普通锦囊牌。<br>"..
  "②当你每轮X种不同的牌名：前/时：此技能①效果失效/其他技能失效。（X为你武将牌上的技能数）<br>"..
  "③出牌阶段限一次，你可以弃置一张牌，然后从牌堆中获取一张与弃置牌相同牌名的牌置于牌堆顶（无则改为随机一张牌），然后你可以将一名角色的手牌数调整至体力上限，若如此做，你将手牌数调整至体力值。若以此法进入弃牌堆的牌数或摸牌牌数之合大于你的体力上限，你失去一点体力",



  ["#tlp_jieyin_xg"] = "解印",
  ["jieyin_viewas"] = "解印",

  ["tlp_jieyin_active"] = "解印",
  [":tlp_jieyin_active"] = " ①锁定技，当你使用牌时，若此时你的手牌数大于/小于你的上家/下家，且此牌有实体：你与其各弃置一张牌/你视为使用一张本回合未以此法使用过的普通锦囊牌。<br>"..
  "②当你轮使用X种不同的牌名：前/时：此技能①效果失效/其他技能失效。（X为你武将牌上的技能数）<br>"..
  "③出牌阶段限一次，你可以弃置一张牌，然后从牌堆中获取一张与弃置牌相同牌名的牌置于牌堆顶（无则改为随机一张牌）然后你可以将一名角色的手牌数调整至体力上限，若如此做，你将手牌数调整至体力值若以此法进入弃牌堆的牌数或摸牌牌数之合大于你的体力上限，你失去一点体力",

}



return jd